package com.charon.common.entity;

import com.charon.common.util.DateUtils;

import java.io.*;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
  *@ClassName: FileInfo
  *@Description: 文件信息实体
  *@Author: Charon [248135724@qq.com]
  *@Date: 2019/1/14 0014 11:19
  *@Version: 1.0
  */
public class FileInfo extends File{

    /**
     * 文件名
     */
    private String fileName;

    /**
     * 文件全名
     */
    private String filePath;

    /**
     * 不带后缀的文件名
     */
    private String name;

    /**
     * 文件扩展名
     */
    private String extension;

    private Date createDate;

    private Date updateDate;

    private long size;

    public FileInfo(String pathname) {
        super(pathname);
        init();
    }

    public FileInfo(String parent, String child) {
        super(parent, child);
        init();
    }

    public FileInfo(File parent, String child) {
        super(parent, child);
        init();
    }

    public FileInfo(URI uri) {
        super(uri);
        init();
    }

    private void init(){
        setCreateDate();
        setExtension();
        setFilePath();
        setFileName();
        setName();
        setUpdateDate();
        setSize();
    }

    public String getFileName() {
        return fileName;
    }

    public String getFilePath() {
        return filePath;
    }

    @Override
    public String getName() {
        return name;
    }

    public String getExtension() {
        return extension;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public long getSize() {
        return size;
    }

    private void setFileName(){
        this.fileName=super.getName();
    }

    private void setFilePath(){
        this.filePath=super.getAbsolutePath();
    }

    private void setName(){
        String n=super.getName();
        if(n.indexOf(".")>-1){
            this.name=n.substring(0,n.lastIndexOf("."));
        }else{
            this.name=n;
        }
    }

    private void setExtension(){
        String n=super.getName();
        if(n.indexOf(".")>-1){
            this.extension=n.substring(n.lastIndexOf("."),n.length());
        }else{
            this.extension=null;
        }
    }

    private void setCreateDate(){
        String strTime = null;
        try {
            Process p = Runtime.getRuntime().exec("cmd /C dir "
                    + super.getAbsoluteFile()
                    + "/tc" );
            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            while((line = br.readLine()) != null){
                if(line.endsWith(".txt")){
                    strTime = line.substring(0,17);
                    break;
                }
            }
        } catch (IOException e) {
            System.err.println("获取创建时间失败！注意：文件创建时间只能在Windows中使用，Linux与Unix系统没有创建时间、更新时间等概念。");
            e.printStackTrace();
        }
        if(strTime!=null){
            Date startTime=DateUtils.parseDate(strTime);
            this.createDate=startTime;
        }else{
            this.createDate=null;
        }
    }

    private void setUpdateDate(){
        Calendar cal = Calendar.getInstance();
        long time = super.lastModified();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cal.setTimeInMillis(time);
        if(0==time){
            this.updateDate=null;
        }else{
            this.updateDate=DateUtils.parseDate(time);
        }
    }

    private void setSize(){
        this.size=super.length();
    }

    @Override
    public String toString() {
        return "FileInfo{" +
                "fileName='" + fileName + '\'' +
                ", filePath='" + filePath + '\'' +
                ", name='" + name + '\'' +
                ", extension='" + extension + '\'' +
                ", createDate=" + createDate +
                ", updateDate=" + updateDate +
                ", size=" + size +
                '}'+super.toString();
    }
}
